# coding=utf-8
# pb zygds
# TODO
'''
快速导入excel
新建--》--开始--》左侧粘贴框--》向下箭头--》使用文本导入向导--》万事大吉
一定要用记事本打开复制
'''

import threading, time, requests, re, json, random, os
import ip_tools

ip_1 = []
ip_2 = []
ip_3 = []
ip_4 = []
ip_con = []


def get_ua():  # 随机UA
    first_num = random.randint(55, 62)
    third_num = random.randint(0, 3200)
    fourth_num = random.randint(0, 140)
    os_type = [
        '(Windows NT 6.1; WOW64)', '(Windows NT 10.0; WOW64)', '(X11; Linux x86_64)',
        '(Macintosh; Intel Mac OS X 10_12_6)'
    ]
    chrome_version = 'Chrome/{}.0.{}.{}'.format(first_num, third_num, fourth_num)
    headers = {
        'User-Agent': 'Mozilla/5.0 %s AppleWebKit/537.36 (KHTML, like Gecko) %s Safari/537.36' % (
            random.choice(os_type), chrome_version)
    }
    return headers


def ip_inf(ip_con):  # 主进程
    ip_con = open(r"iplist.txt", 'r', encoding='utf-8').readlines()
    for i in range(0, (len(ip_con))):
        try:
            ip_inf_tmp = (requests.get("http://ip-api.com/json/%s?lang=zh-CN" % ip_con[i].replace('\n', ''))).json()
            with open("ip_inf.txt", "a", encoding="utf-8") as filea:
                filea.write(
                    ip_con[i] + ";" + ip_inf_tmp["country"] + ";" + ip_inf_tmp["regionName"] + ";" + ip_inf_tmp[
                        "city"] + "\n")
            print("现在是第%s条数据" % (i + 1))
        except:
            try:
                time.sleep(5)
                ip_inf_tmp = (requests.get("http://ip-api.com/json/%s?lang=zh-CN" % ip_con[i].replace('\n', ''))).json()
                with open("ip_inf.txt", "a", encoding="utf-8") as filea:
                    filea.write(
                        ip_con[i] + ";" + ip_inf_tmp["country"] + ";" + ip_inf_tmp["regionName"] + ";" + ip_inf_tmp[
                            "city"] + "\n")
                print("现在是第%s条数据" % (i + 1))
            except:
                time.sleep(5)
                ip_inf_tmp = (requests.get("http://ip-api.com/json/%s?lang=zh-CN" % ip_con[i].replace('\n', ''))).json()
                with open("ip_inf.txt", "a", encoding="utf-8") as filea:
                    filea.write(
                        ip_con[i] + ";" + ip_inf_tmp["country"] + ";" + ip_inf_tmp["regionName"] + ";" + ip_inf_tmp[
                            "city"] + "\n")
                print("现在是第%s条数据" % (i + 1))


def ip_inf_1(ip_1):
    for i in range(0, (len(ip_1))):
        try:
            ip_inf_1_tmp = (requests.get("http://ip-api.com/json/%s?lang=zh-CN" % ip_1[i])).json()
            with open("ip_inf.txt", "a", encoding="utf-8") as file1:
                file1.write(
                    ip_1[i] + ";" + ip_inf_1_tmp["country"] + ";" + ip_inf_1_tmp["regionName"] + ";" + ip_inf_1_tmp[
                        "city"] + "\n")
            print("现在是1线程第%s条数据" % (i + 1))
        except:
            try:
                time.sleep(5)
                with open("ip_inf.txt", "a", encoding="utf-8") as file1:
                    file1.write(
                        ip_1[i] + ";" + ip_inf_1_tmp["country"] + ";" + ip_inf_1_tmp["regionName"] + ";" + ip_inf_1_tmp[
                            "city"] + "\n")
                print("现在是1线程第%s条数据" % (i + 1))
            except:
                try:
                    time.sleep(5)
                    with open("ip_inf.txt", "a", encoding="utf-8") as file1:
                        file1.write(
                            ip_1[i] + ";" + ip_inf_1_tmp["country"] + ";" + ip_inf_1_tmp["regionName"] + ";" +
                            ip_inf_1_tmp[
                                "city"] + "\n")
                    print("现在是1线程第%s条数据" % (i + 1))
                except:
                    time.sleep(5)
                    with open("ip_inf.txt", "a", encoding="utf-8") as file1:
                        file1.write(
                            ip_1[i] + ";" + ip_inf_1_tmp["country"] + ";" + ip_inf_1_tmp["regionName"] + ";" +
                            ip_inf_1_tmp[
                                "city"] + "\n")
                    print("现在是1线程第%s条数据" % (i + 1))


def ip_inf_2(ip_2):
    for i in range(0, (len(ip_1))):
        try:
            ip_inf_2_tmp = (requests.get("http://ip-api.com/json/%s?lang=zh-CN" % ip_2[i])).json()
            with open("ip_inf.txt", "a", encoding="utf-8") as file2:
                file2.write(
                    ip_1[i] + ";" + ip_inf_2_tmp["country"] + ";" + ip_inf_2_tmp["regionName"] + ";" + ip_inf_2_tmp[
                        "city"] + "\n")
            print("现在是2线程第%s条数据" % (i + 1))
        except:
            try:
                time.sleep(5)
                ip_inf_2_tmp = (requests.get("http://ip-api.com/json/%s?lang=zh-CN" % ip_2[i])).json()
                with open("ip_inf.txt", "a", encoding="utf-8") as file2:
                    file2.write(
                        ip_1[i] + ";" + ip_inf_2_tmp["country"] + ";" + ip_inf_2_tmp["regionName"] + ";" + ip_inf_2_tmp[
                            "city"] + "\n")
                print("现在是2线程第%s条数据" % (i + 1))
            except:
                try:
                    time.sleep(5)
                    ip_inf_2_tmp = (requests.get("http://ip-api.com/json/%s?lang=zh-CN" % ip_2[i])).json()
                    with open("ip_inf.txt", "a", encoding="utf-8") as file2:
                        file2.write(
                            ip_1[i] + ";" + ip_inf_2_tmp["country"] + ";" + ip_inf_2_tmp["regionName"] + ";" +
                            ip_inf_2_tmp[
                                "city"] + "\n")
                    print("现在是2线程第%s条数据" % (i + 1))
                except:
                    time.sleep(5)
                    ip_inf_2_tmp = (requests.get("http://ip-api.com/json/%s?lang=zh-CN" % ip_2[i])).json()
                    with open("ip_inf.txt", "a", encoding="utf-8") as file2:
                        file2.write(
                            ip_1[i] + ";" + ip_inf_2_tmp["country"] + ";" + ip_inf_2_tmp["regionName"] + ";" +
                            ip_inf_2_tmp[
                                "city"] + "\n")
                    print("现在是2线程第%s条数据" % (i + 1))


def ip_inf_3(ip_3):
    for i in range(0, (len(ip_1))):
        try:
            ip_inf_3_tmp = ((requests.get("http://ip-api.com/json/%s?lang=zh-CN" % ip_3[i])).json())
            with open("ip_inf.txt", "a", encoding="utf-8") as file3:
                file3.write(
                    ip_1[i] + ";" + ip_inf_3_tmp["country"] + ";" + ip_inf_3_tmp["regionName"] + ";" + ip_inf_3_tmp[
                        "city"] + "\n")
            print("现在是3线程第%s条数据" % (i + 1))
        except:
            try:
                time.sleep(5)
                ip_inf_3_tmp = ((requests.get("http://ip-api.com/json/%s?lang=zh-CN" % ip_3[i])).json())
                with open("ip_inf.txt", "a", encoding="utf-8") as file3:
                    file3.write(
                        ip_1[i] + ";" + ip_inf_3_tmp["country"] + ";" + ip_inf_3_tmp["regionName"] + ";" + ip_inf_3_tmp[
                            "city"] + "\n")
                print("现在是3线程第%s条数据" % (i + 1))
            except:
                try:
                    time.sleep(5)
                    ip_inf_3_tmp = ((requests.get("http://ip-api.com/json/%s?lang=zh-CN" % ip_3[i])).json())
                    with open("ip_inf.txt", "a", encoding="utf-8") as file3:
                        file3.write(
                            ip_1[i] + ";" + ip_inf_3_tmp["country"] + ";" + ip_inf_3_tmp["regionName"] + ";" +
                            ip_inf_3_tmp[
                                "city"] + "\n")
                    print("现在是3线程第%s条数据" % (i + 1))
                except:
                    time.sleep(5)
                    ip_inf_3_tmp = ((requests.get("http://ip-api.com/json/%s?lang=zh-CN" % ip_3[i])).json())
                    with open("ip_inf.txt", "a", encoding="utf-8") as file3:
                        file3.write(
                            ip_1[i] + ";" + ip_inf_3_tmp["country"] + ";" + ip_inf_3_tmp["regionName"] + ";" +
                            ip_inf_3_tmp[
                                "city"] + "\n")
                    print("现在是3线程第%s条数据" % (i + 1))


def ip_inf_4(ip_4):
    for i in range(0, (len(ip_1))):
        try:
            ip_inf_4_tmp = ((requests.get("http://ip-api.com/json/%s?lang=zh-CN" % ip_4[i])).json())
            with open("ip_inf.txt", "a", encoding="utf-8") as file4:
                file4.write(
                    ip_1[i] + ";" + ip_inf_4_tmp["country"] + ";" + ip_inf_4_tmp["regionName"] + ";" + ip_inf_4_tmp[
                        "city"] + "\n")
            print("现在是4线程第%s条数据" % (i + 1))
        except:
            try:
                time.sleep(5)
                ip_inf_4_tmp = ((requests.get("http://ip-api.com/json/%s?lang=zh-CN" % ip_4[i])).json())
                with open("ip_inf.txt", "a", encoding="utf-8") as file4:
                    file4.write(
                        ip_1[i] + ";" + ip_inf_4_tmp["country"] + ";" + ip_inf_4_tmp["regionName"] + ";" + ip_inf_4_tmp[
                            "city"] + "\n")
                print("现在是4线程第%s条数据" % (i + 1))
            except:
                try:
                    time.sleep(5)
                    ip_inf_4_tmp = ((requests.get("http://ip-api.com/json/%s?lang=zh-CN" % ip_4[i])).json())
                    with open("ip_inf.txt", "a", encoding="utf-8") as file4:
                        file4.write(
                            ip_1[i] + ";" + ip_inf_4_tmp["country"] + ";" + ip_inf_4_tmp["regionName"] + ";" +
                            ip_inf_4_tmp[
                                "city"] + "\n")
                    print("现在是4线程第%s条数据" % (i + 1))
                except:
                    time.sleep(5)
                    ip_inf_4_tmp = ((requests.get("http://ip-api.com/json/%s?lang=zh-CN" % ip_4[i])).json())
                    with open("ip_inf.txt", "a", encoding="utf-8") as file4:
                        file4.write(
                            ip_1[i] + ";" + ip_inf_4_tmp["country"] + ";" + ip_inf_4_tmp["regionName"] + ";" +
                            ip_inf_4_tmp[
                                "city"] + "\n")
                    print("现在是4线程第%s条数据" % (i + 1))


# def tongji():
#     tj_inf = open(r"ip_inf.txt", 'r', encoding='utf-8').readlines()
#     print(tj_inf)
#     print(tj_inf.count('China'))


def main():  # 多线程使用
    # 初始化IP文本信息
    with open("ip_inf.txt", "w", encoding="utf-8") as file:
        pass
    ip_tools.ip_listto4.ip_list(ip_1, ip_2, ip_3, ip_4)
    t1 = threading.Thread(target=ip_inf_1, args=(ip_1,))
    t2 = threading.Thread(target=ip_inf_2, args=(ip_2,))
    t3 = threading.Thread(target=ip_inf_3, args=(ip_3,))
    t4 = threading.Thread(target=ip_inf_4, args=(ip_4,))
    t4.start()
    t3.start()
    t2.start()
    t1.start()


if __name__ == '__main__':
    # os.popen('msg %username% /time:10 "请不要使用代理，注意看脚本提示"')
    # ip_inf(ip_con)    # 如果使用代理则注释下面那行并取消这行注释，但是不推荐
    main()
    # tongji()
    # TODO 统计个屁，导进EXCEL直接出数据，fuck，麻烦死了
